﻿using BLL;
using Model;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 新增客户 : Sunny.UI.UIForm
    {
        public LXR LinkMan = new LXR();
        public FormUseful  Useful=FormUseful.新增 ;
        public 新增客户()
        {
            InitializeComponent();
            comboBoxEx1.DataSource = Tools.获取模板.获取所有模板 (PrintPath.唛头模板);
            cmbOwn.Text = Tools.CurrentUser.user.YHBH;
            var yonghulist = YhbService.GetYhblst();
            foreach (var yonghu in yonghulist )
            {
                cmbOwn.Items.Add(yonghu.YHBH + "/" + yonghu.YHMC);
            }
            cmbtitle.DataSource = infoService.Getinfolst().Select(x => x.gsmc).ToList();
        }


        private void InitText()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevComponents.DotNetBar.Controls.TextBoxX)
                {
                    c.Text = "";
                }
            }
            LinkMan.BH = BianhaoBLL.CreatBianhao("KH");
            LinkMan.LX = "客户";
            LinkMan.sxed = 0;
            LinkMan.fp = 0;
            LinkMan.JE = 0;
            txtBianhao.Text = LinkMan.BH;
            txtqichu.Text = "0";
            txtyingkaifapiao.Text = "0";
            txtzhangqi.Text = "120";
            txtedu.Text = "100000";
            txtusd.Text = "0.00";
            txtFullName.Text = string.Empty;
            checkBox1.Checked = false;
            comboBoxEx1.Text = QueryTime.DefaultShippingMark;
        }

        private void 新增品种_Load(object sender, EventArgs e)
        {
            if (Useful == FormUseful.新增)
            {
                InitText();
            }
            else
            {
                EditText();
            }

        }
        private void EditText()
        {
            txtBianhao.Text = LinkMan.BH;
            txtbeizhu.Text = LinkMan.bz;
            txtdizhi.Text = LinkMan.dz;
            txtlianxidianhua.Text = LinkMan.DH;
            txtlianxiren.Text = LinkMan.Lxr;
            txtyingkaifapiao.Text = LinkMan.fp.ToString();
            txtyingkaifapiao.Text = LinkMan.YingKaifapiao.ToString();
            txtyouxiang.Text = LinkMan.YX;
            txtqq.Text = LinkMan.QQ;
            txtzhangqi.Text = LinkMan.ZhangQi.ToString();
            txtweixing.Text = LinkMan.WX;
            txtpingyingma.Text = LinkMan.ZJC;
            txtqichu.Text = LinkMan.JE.ToString();
            txtshuihao.Text = LinkMan.Shuihao;
            txtyinghangzhanghao.Text = LinkMan.Yinghangzhanghao;
            txtkaihuyinghang.Text = LinkMan.Kaihuyinghang;
            txtpingming.Text = LinkMan.MC;
            txtedu.Text = LinkMan.sxed.ToString();
            txtusd.Text = LinkMan.USD.ToString();
            txtmashu.Text = LinkMan.Mashu.ToString();
            cmbdanwei.Text = LinkMan.Danwei;
            txtFullName.Text = LinkMan.FullName;
            checkBox1.Checked = LinkMan.DefualtTax;
            comboBoxEx1.Text= MaitouService.GetOneMaitou(x=>x.khbh==LinkMan.BH ).path ;
            cmbtitle.Text =string.IsNullOrEmpty( LinkMan.DefualtDocTitle)?QueryTime.DefaultTitle: LinkMan.DefualtDocTitle;
        }
        private void InitPingzhong()
        {
            LinkMan.BH = txtBianhao.Text;
            LinkMan.bz = txtbeizhu.Text;
            LinkMan.dz = txtdizhi.Text;
            LinkMan.DH = txtlianxidianhua.Text;
            LinkMan.Lxr  = txtlianxiren.Text;
            LinkMan.fp = txtyingkaifapiao.Text.ToDecimal(0);
            LinkMan.YingKaifapiao = txtyingkaifapiao.Text.ToDecimal(0);
            LinkMan.own = cmbOwn.Text.Split ('/')[0];
            LinkMan.YX = txtyouxiang.Text;
            LinkMan.QQ = txtqq.Text;
            LinkMan.ZhangQi = txtzhangqi.Text.ToInt ();
            LinkMan.WX = txtweixing.Text;
            LinkMan.ZJC = txtpingyingma.Text;
            LinkMan.JE = txtqichu.Text.ToDecimal (0);
            LinkMan.Shuihao = txtshuihao.Text;
            LinkMan.Yinghangzhanghao = txtyinghangzhanghao.Text;
            LinkMan.Kaihuyinghang = txtkaihuyinghang.Text;
            LinkMan.MC  = txtpingming.Text.TrimEnd (' ');
            LinkMan.sxed = txtedu.Text.ToDecimal(0);
            LinkMan.USD = txtusd.Text.TryToDecmial();
            LinkMan.Mashu = txtmashu.Text.TryToDecmial(0);
            LinkMan.Danwei = cmbdanwei.Text;
            LinkMan.FullName = txtFullName.Text;
            LinkMan.DefualtTax = checkBox1.Checked;
            LinkMan.DefualtDocTitle = cmbtitle.Text;
        }
        private Boolean CheckTelLegitimacy(string Tel)
        {
            if (QueryTime.IsMustNeedTel)
            {
                if (string.IsNullOrEmpty(txtlianxidianhua.Text))
                {
                    Tools.ShowMessageBox.ShowErrorBox("当前策略为客户必须要有联系电话！检测不到该客户的联系电话！");
                    return true;
                }
                else
                {
                    if(Tel.Length<11)
                    {
                        ShowMessageBox.ShowErrorBox("电话输入的格式有误！长度需要11位");
                        return true;
                    }
                    if(Tel.ObjToDecimal()==0)
                    {
                        ShowMessageBox.ShowErrorBox("电话输入的格式有误！电话号码必须是数字");
                        return true;
                    }
                    return false;
                }
            }
            else
            {
                return false;
            }

        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtpingming.Text))
                {
                    ShowMessageBox.ShowErrorBox("请输入客户名称！客户名称不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(comboBoxEx1.Text))
                {
                    ShowMessageBox.ShowErrorBox("请选择客户的默认唛头模板");
                    return;
                }
                if (string.IsNullOrWhiteSpace(cmbtitle.Text))
                {
                    ShowMessageBox.ShowErrorBox("请选择客户的默认发货公司抬头");
                    return;
                }
                if (CheckTelLegitimacy(txtlianxidianhua.Text))
                {
                    return;
                }
                InitPingzhong();
                if (Useful == FormUseful.新增)
                {
                    if (!string.IsNullOrWhiteSpace(LXRService.GetOneLXR(x => x.MC == txtpingming.Text&&x.Leixing=="客户").MC))
                    {
                        ShowMessageBox.ShowErrorBox("该客户名称已经存在！保存失败");
                        return;
                    }
                    LXRService.InsertLXR(LinkMan);
                    MaitouService.InsertMaitou(new Maitou() { khbh = LinkMan.BH, path = comboBoxEx1.Text, own = Tools.CurrentUser.user.YHBH });
                }
                else
                {
                    var linkman = LXRService.GetOneLXR(x => x.BH == txtBianhao.Text);
                    try
                    {
                        Connect.dbcontext.BeginTran();
                        if (linkman.MC != this.LinkMan.MC)
                        {
                            Connect.dbcontext.Ado.ExecuteCommand($"Update LwDetail set KHMC='{txtpingming.Text}' where KHMC='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update DanjuTable set ksmc='{txtpingming.Text}' where ksmc='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update danjumingxitable set CustomName='{txtpingming.Text}' where CustomName='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update liuzhuancard set Customer='{txtpingming.Text}' where Customer='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update StockTable set CustomName='{txtpingming.Text}' where CustomName='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update JuanHaoTable set CustomerName='{txtpingming.Text}' where CustomerName='{linkman.MC}'");
                            Connect.dbcontext.Ado.ExecuteCommand($"Update RukuTable set CustomName='{txtpingming.Text}' where CustomName='{linkman.MC}'");
                        }
                        //StockTableService.UpdateStockTable(x => x.CustomName == txtpingming.Text, x => x.CustomName ==linkman.MC);

                        Connect.dbcontext.Updateable(this.LinkMan).ExecuteCommand();
                        MaitouService.UpdateMaitou(new Maitou() { khbh = LinkMan.BH, path = comboBoxEx1.Text, own = Tools.CurrentUser.user.YHBH }, x => x.khbh == LinkMan.BH);
                        Connect.dbcontext.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        Connect.dbcontext.RollbackTran();
                        ShowMessageBox.ShowErrorBox("修改客户信息的时候发生了错误:" + ex.Message);
                    }
                }
                if (ShowMessageBox.ShowAskBox("保存成功！是否继续添加？") == DialogResult.Yes)
                {
                    LinkMan = new LXR() { LX = "客户" };
                    Useful = FormUseful.新增;
                    InitText();
                }
                else
                {
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }

        private void txtpingming_TextChanged(object sender, EventArgs e)
        {
          txtpingyingma.Text =  Tools.GetPy.GetPingYingFirstLetter (txtpingming.Text);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new InpuBox() { Label = "请输入你要新建的模板名称", 内容 = "", filePath = PrintPath.唛头模板 };
            fm.ShowDialog();
            if (fm.内容 != "")
            {
                Tools.获取模板.新增模板(PrintPath.唛头模板 , fm.内容,fm.参考模板, ReportService.唛头);
                comboBoxEx1.DataSource = Tools.获取模板.获取所有模板(PrintPath.唛头模板);
            }
        }
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (后整理管理系统.自定义类.PrintService.CheckPrintDesignAccess())
            {
                var f = new 打印设置窗体();
                //fm.ShowDialog();
                new Tools.打印唛头() { copyies = f.copyies, PrinterName = f.printer, userful = PrintModel.Design, moban = PrintPath.唛头模板 + comboBoxEx1.Text, juan = new JuanHaoTable() }.打印();
            }
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var l = MaitouService.GetMaitoulst(x => x.path == comboBoxEx1.Text);
            if(l.Count>0)
            {
                MessageBox.Show("该唛头已经关联到一些客户。请先解绑！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Tools.获取模板.删除模板(PrintPath.唛头模板+comboBoxEx1.Text );
            comboBoxEx1.DataSource = Tools.获取模板.获取所有模板(PrintPath.唛头模板);
        }
    }
}
